{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find similar tags\n",
    "\n",
    "\n",
    "## Prepare\n",
    "\n",
    "- Download pre-computed tag occurrence and tag co-occurrence data\n",
    "```\n",
    "cd $HOME/VisualSearch\n",
    "wget http://lixirong.net/data/csur2016/train1m-tagfreq.tar.gz\n",
    "tar xzf train1m-tagfreq.tar.gz\n",
    "```\n",
    "\n",
    "- Setup the Jingwei environment\n",
    "```\n",
    "source ~/github/jingwei/start.sh\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[tagsim.flickr_similarity.FlickrContextSim] 1198818 images, 347369 users, 62862 tags\n",
      "[tagsim.flickr_similarity.FlickrContextSim] 4552497 tag pairs\n"
     ]
    }
   ],
   "source": [
    "from util.tagsim.flickr_similarity import FlickrContextSim\n",
    "fcs = FlickrContextSim('train1m')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.719895795351 0.595356835123 0.210904755059\n"
     ]
    }
   ],
   "source": [
    "# compute similarity for a given pair of tags\n",
    "print fcs.compute('boeing', 'airplane'), fcs.compute('boeing', 'flight'), fcs.compute('boeing', 'street')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('boeing', 1.0), ('airliner', 0.8043727834468062), ('747', 0.7838391722717928), ('airline', 0.7763174382609282), ('737', 0.7659568878683966), ('jet', 0.7334135162779946), ('aviation', 0.7331368438285586), ('aircraft', 0.7311557033886409), ('jetliner', 0.7290937741145928), ('airplane', 0.719895795350634)]\n"
     ]
    }
   ],
   "source": [
    "# find similar tags in a given vocabulary\n",
    "vob = fcs.vob\n",
    "query_tag = 'boeing'\n",
    "tagscores = [(tag, fcs.compute(tag, query_tag)) for tag in vob]\n",
    "tagscores.sort(key=lambda v:v[1], reverse=True)\n",
    "# show the top 10 ranked tags\n",
    "print tagscores[:10]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
